package com.elong.businesstravel.base.baidusdk.map;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMyLocationClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 百度mapview
 * 
 * @author jiahongfei
 * 
 */
public class BaiduMapView {

	public interface LocationOverlayDispatchTap {
		public boolean dispatchTap();
	}

	private Context mContext;
	private LocationOverlayDispatchTap mLocationOverlayDispatchTap;
	private MapView mMapView;
	private BaiduMap mBaiduMap;

	public BaiduMapView(Context context, MapView mapView) {
		mContext = context;
		mMapView = mapView;
		mBaiduMap = mMapView.getMap();

		initBaiduMapView();

		initLocationOverlay();
	}

	private void initBaiduMapView() {
		// 地图初始化
		MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(16);
		mBaiduMap.animateMapStatus(u);

	}

	private void initLocationOverlay() {
		// 开启定位图层
		mBaiduMap.setMyLocationEnabled(true);
		mBaiduMap.setOnMyLocationClickListener(new OnMyLocationClickListener() {

			@Override
			public boolean onMyLocationClick() {
				if (null != mLocationOverlayDispatchTap) {
					return mLocationOverlayDispatchTap.dispatchTap();
				}
				return false;
			}
		});
	}

	/**
	 * 设置定位数据
	 * 
	 * @param locData
	 */
	public void setLocationOverlayData(MyLocationData locData) {
		// myLocationOverlay.setData(locData);
		mBaiduMap.setMyLocationData(locData);
	}

	// public void setLocationMode(LocationMode locationMode) {
	// myLocationOverlay.setLocationMode(locationMode);
	// }

	/**
	 * 修改位置图标
	 * 
	 * @param marker
	 */
	public void modifyLocationOverlayIcon(int markerId) {
		// // 当传入marker为null时，使用默认图标绘制
		// myLocationOverlay.setMarker(marker);
		// // 修改图层，需要刷新MapView生效
		// mMapView.refresh();
		BitmapDescriptor marker = BitmapDescriptorFactory
				.fromResource(markerId);
		mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
				LocationMode.NORMAL, true, marker));
	}
	
	public BaiduMap getBaiduMap(){
		return mBaiduMap;
	}

	/**
	 * 设置定位图层点击
	 * 
	 * @param locationOverlayDispatchTap
	 */
	public void setLocationOverlayDispatchTap(
			LocationOverlayDispatchTap locationOverlayDispatchTap) {
		mLocationOverlayDispatchTap = locationOverlayDispatchTap;

	}

}
